package qreasymarket.movil;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class CapturaQRActivity extends Activity {

	private Button btnQR;
	TextView captura;
	EditText emailactual;

	// String capturas;
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.captura_qr);

		btnQR = (Button) findViewById(R.id.btnQR);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		btnQR.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(
						"com.google.zxing.client.android.SCAN");
				intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
				startActivityForResult(intent, 0);

			}

		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu1) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menuprincipal, menu1);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.PromocionService) {
			startActivity(new Intent(CapturaQRActivity.this, CarruselActivity.class));
			return (true);
		}

		return (super.onOptionsItemSelected(item));
	}

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {

		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {
				String contents = intent.getStringExtra("SCAN_RESULT");
				// String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
				// Handle successful scan
				System.out.println(contents);

				RestClient cliente = new RestClient(
						"http://192.168.1.176:8080/QR-EasyMarket_Web/rest/qr_buscar");
				cliente.AddParam("qr", contents);
				try {
					cliente.Execute(RequestMethod.GET);
					String jsontext = cliente.getResponse();

					JSONArray entries = new JSONArray(jsontext);
					jsontext = jsontext.toLowerCase().trim();

					int b;

					EditText emailActual = (EditText) findViewById(R.id.email_user_a);
					String emailA = emailActual.getText().toString();

					for (b = 0; b < entries.length(); b++) {
						JSONObject post = entries.getJSONObject(b);
						String encontrados = post.getString("codigoQr");
						String idproducto = post.getString("idProducto");
						String nombreprod = post.getString("nombreProducto");
						String precios = post.getString("precio");
						String promocion = post.getString("promocionProducto");

						if (contents.equals(encontrados)) {
							Intent i = new Intent(CapturaQRActivity.this,
									ProductoActivity.class);
							Bundle producto = new Bundle();
							producto.putString("id", idproducto);
							producto.putString("emailActual", emailA);
							producto.putString("proname", nombreprod);
							producto.putString("preciop", precios);
							producto.putString("promopro", promocion);

							i.putExtras(producto);

							startActivity(i);
						} else if (resultCode == RESULT_CANCELED) {

						}
					}

				} catch (Exception e) {
					Log.i("CapturaQRActivity", "ERROR:" + e.getMessage());
				}

			}
		}
	}
}
